<?php

class MenusController extends BaseAdminController {


	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Menus'),
		));
	}

	public function actionCreate() {
		$model = new Menus;


		if (isset($_POST['Menus'])) {
			$model->attributes = $_POST['Menus'];

			if ($model->save()) {
				if (Yii::app()->request->isAjaxRequest)
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'Menus');


		if (isset($_POST['Menus'])) {
			$model->attributes = $_POST['Menus'];

			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->request->isPostRequest) {
			$model = $this->loadModel($id, 'Menus');
			if($model->delete()){
				MenuItems::model()->deleteAll('menu_group=:g', array(':g'=>$id));
				if (!Yii::app()->request->isAjaxRequest)
					$this->redirect(array('admin'));
			}
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Menus');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new Menus('search');
		$model->unsetAttributes();

		if (isset($_GET['Menus']))
			$model->attributes = $_GET['Menus'];

		$this->render('admin', array(
			'model' => $model,
		));
	}

}